﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: WinsorizedMean.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-09-06 11:39 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;

namespace BOL.Linq.Descriptive
{
    public static partial class Descriptive
    {
        public static decimal WinsorizedMean(this IEnumerable<decimal> source, double lp, double hp)
        {
            return (decimal)source.Select(x => (double)x).WinsorizedMean(lp, hp);
        }

        public static double WinsorizedMean(this IEnumerable<double> source, double lp, double hp)
        {
            if (lp < 0 || lp > 1 || hp < 0 || hp > 1 || hp < lp)
                throw new ArgumentOutOfRangeException();

            var list = source.ToList();
            var count = list.Count();
            var ln = (int)Math.Round(count * lp);
            var hn = (int)Math.Round(count * (1 - hp));
            var n = ln + hn;

            if (n > 0)
            {
                var sorted = list.OrderBy(x => x);
                var winsorized = sorted.Skip(ln).Take(count - n).ToList();
                var lower = Enumerable.Repeat(winsorized[0], ln);
                var upper = Enumerable.Repeat(winsorized[count - n - 1], hn);

                return lower.Concat(winsorized).Concat(upper).Average();
            }

            return list.Average();
        }

        public static float WinsorizedMean(this IEnumerable<float> source, double lp, double hp)
        {
            return (float)source.Select(x => (double)x).WinsorizedMean(lp, hp);
        }

        public static double WinsorizedMean(this IEnumerable<int> source, double lp, double hp)
        {
            return source.Select(x => (double)x).WinsorizedMean(lp, hp);
        }

        public static double WinsorizedMean(this IEnumerable<long> source, double lp, double hp)
        {
            return source.Select(x => (double)x).WinsorizedMean(lp, hp);
        }

        public static decimal WinsorizedMean<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector, double lp, double hp)
        {
            return source.Select(selector).WinsorizedMean(lp, hp);
        }

        public static double WinsorizedMean<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector, double lp, double hp)
        {
            return source.Select(selector).WinsorizedMean(lp, hp);
        }

        public static float WinsorizedMean<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector, double lp, double hp)
        {
            return source.Select(selector).WinsorizedMean(lp, hp);
        }

        public static double WinsorizedMean<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector, double lp, double hp)
        {
            return source.Select(selector).WinsorizedMean(lp, hp);
        }

        public static double WinsorizedMean<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector, double lp, double hp)
        {
            return source.Select(selector).WinsorizedMean(lp, hp);
        }
    }
}
